//
//  AIListCell.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/7/31.
//

#import "AIListCell.h"

@interface AIListCell()

@property(nonatomic,strong)UILabel *tipLabel;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *contentLabel;
@property(nonatomic,strong)UIImageView *voiceImageView;
@property(nonatomic,strong)UILabel *titleLabel2;
@property(nonatomic,strong)UILabel *contentLabel2;
@property(nonatomic,strong)UIImageView *voiceImageView2;
@property(nonatomic,strong)UIView *line;
@property(nonatomic,strong)UIButton *editBtn;
@property(nonatomic,strong)UIButton *cBtn;
@property(nonatomic,strong)UIButton *shareBtn;
@property(nonatomic,strong)UIImageView *loadingImageView;
@property(nonatomic,strong)UIView *bgView;

@end

@implementation AIListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    self.backgroundColor = UIColorHex(0xF2F7F6);
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.right.mas_equalTo(@-16);
        make.top.mas_equalTo(@8);
        make.bottom.mas_equalTo(@-8);
    }];
    self.bgView = bgView;
    
    if(!self.currentAvailableWidth){
        self.currentAvailableWidth = kScreenWidth;
    }
    self.tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.currentAvailableWidth-32,0,20,23)];
    self.tipLabel.font = JPUIFontPingFangSCRegular(12);
    self.tipLabel.textColor = KThemeColor;
    self.tipLabel.textAlignment = NSTextAlignmentCenter;
    self.tipLabel.backgroundColor = UIColorHex(0xE8FAF5);
    [bgView addSubview:self.tipLabel];
    self.tipLabel.hidden = YES;
  
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.font = JPUIFontPingFangSCRegular(14);
    self.titleLabel.textColor = KFontBlackColor;
    [bgView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.top.mas_equalTo(@16);
        make.height.mas_equalTo(@20);
    }];
    self.contentLabel = [[UILabel alloc]init];
    self.contentLabel.font = JPUIFontPingFangSCRegular(14);
    self.contentLabel.textColor = KFontGrayColor;
    self.contentLabel.numberOfLines = 0;
    [bgView addSubview:self.contentLabel];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.right.mas_equalTo(@-16);
        make.height.mas_equalTo(@0);
        make.top.mas_equalTo(@48);
    }];
    
    __weak typeof(self)weakSelf = self;
    self.voiceImageView = [[UIImageView alloc]init];
    self.voiceImageView.image = [UIImage imageNamed:@"mint_mycollect_play"];
    self.voiceImageView.userInteractionEnabled = YES;
    [bgView addSubview:self.voiceImageView];
    [self.voiceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@20);
        make.left.mas_equalTo(@16);
        make.top.mas_equalTo(weakSelf.contentLabel.mas_bottom).offset(12);
    }];
    UITapGestureRecognizer *playTap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(playTap:andIndex:)]){
            [weakSelf.delegate playTap:weakSelf andIndex:0];
        }
    }];
    [self.voiceImageView addGestureRecognizer:playTap];
    
    self.editBtn = [[UIButton alloc]init];
    [self.editBtn setImage:[UIImage imageNamed:@"ai_edit_logo"] forState:UIControlStateNormal];
    [self.editBtn addTarget:self action:@selector(editBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:self.editBtn];
    [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(@-11);
        make.width.height.mas_equalTo(@28);
        make.centerY.mas_equalTo(weakSelf.voiceImageView);
    }];
    self.editBtn.hidden = YES;
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = KLineColor;
    [bgView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.right.mas_equalTo(@-16);
        make.height.mas_equalTo(@1);
        make.top.mas_equalTo(weakSelf.voiceImageView.mas_bottom).offset(12);
    }];
    
    self.titleLabel2 = [[UILabel alloc]init];
    self.titleLabel2.font = JPUIFontPingFangSCRegular(14);
    self.titleLabel2.textColor = KFontGrayColor;
    [bgView addSubview:self.titleLabel2];
    [self.titleLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.top.mas_equalTo(weakSelf.line.mas_bottom).offset(12);
        make.height.mas_equalTo(@20);
    }];
    self.contentLabel2 = [[UILabel alloc]init];
    self.contentLabel2.font = JPUIFontPingFangSCRegular(14);
    self.contentLabel2.textColor = KFontBlackColor;
    self.contentLabel2.numberOfLines = 0;
    [bgView addSubview:self.contentLabel2];
    [self.contentLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.right.mas_equalTo(@-16);
        make.height.mas_equalTo(@0);
        make.top.mas_equalTo(weakSelf.titleLabel2.mas_bottom).offset(12);
    }];
    
    self.voiceImageView2 = [[UIImageView alloc]init];
    self.voiceImageView2.image = [UIImage imageNamed:@"mint_mycollect_play"];
    self.voiceImageView2.userInteractionEnabled = YES;
    [bgView addSubview:self.voiceImageView2];
    [self.voiceImageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@20);
        make.left.mas_equalTo(@16);
        make.top.mas_equalTo(weakSelf.contentLabel2.mas_bottom).offset(12);
    }];
    UITapGestureRecognizer *playTap2 = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(playTap:andIndex:)]){
            [weakSelf.delegate playTap:weakSelf andIndex:1];
        }
    }];
    [self.voiceImageView2 addGestureRecognizer:playTap2];
    
    self.shareBtn = [[UIButton alloc]init];
    [self.shareBtn setImage:[UIImage imageNamed:@"ai_share_logo"] forState:UIControlStateNormal];
    [self.shareBtn addTarget:self action:@selector(shareBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:self.shareBtn];
    [self.shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(@-11);
        make.width.height.mas_equalTo(@28);
        make.centerY.mas_equalTo(weakSelf.voiceImageView2);
    }];
    self.shareBtn.hidden = YES;
    
    self.cBtn = [[UIButton alloc]init];
    [self.cBtn setImage:[UIImage imageNamed:@"ai_copy_logo"] forState:UIControlStateNormal];
    [self.cBtn addTarget:self action:@selector(cBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:self.cBtn];
    [self.cBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakSelf.shareBtn.mas_left).offset(-14);
        make.width.height.mas_equalTo(@28);
        make.centerY.mas_equalTo(weakSelf.voiceImageView2);
    }];
    self.cBtn.hidden = YES;
    
    
}

#pragma mark - 编辑
-(void)editBtnClick{
    if (self.delegate && [self.delegate respondsToSelector:@selector(editTap:)]){
        [self.delegate editTap:self];
    }
}

#pragma mark - 分享
-(void)shareBtnClick{
    if (self.delegate && [self.delegate respondsToSelector:@selector(shareTap:)]){
        [self.delegate shareTap:self];
    }
}

#pragma mark - 复制
-(void)cBtnClick{
    if(_model.receiveContent){
        [UIPasteboard generalPasteboard].string = _model.receiveContent;
        [JPProgressHUD showMsg:@"复制成功"];
    }
}

-(void)setModel:(AIListModel *)model{
    _model = model;
    __weak typeof(self)weakSelf = self;
    if(model.isExample == YES){
        self.titleLabel.text = @"示例";
        self.cBtn.hidden = YES;
        self.editBtn.hidden = YES;
        self.shareBtn.hidden = YES;
        if(self.loadingImageView){
            self.loadingImageView.hidden = YES;
        }
        self.voiceImageView2.hidden = NO;
        self.contentLabel2.hidden = NO;
    }else{
        self.titleLabel.text = @"原文";
        self.editBtn.hidden = NO;
        if(model.isLoad == YES){
            self.contentLabel2.hidden = YES;
            self.voiceImageView2.hidden = YES;
            self.cBtn.hidden = YES;
            self.shareBtn.hidden = YES;
            if(!self.loadingImageView){
                self.loadingImageView = [[UIImageView alloc]init];
                [self.bgView addSubview:self.loadingImageView];
                [self.loadingImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(@16);
                    make.width.mas_equalTo(@50);
                    make.height.mas_equalTo(@31);
                    make.top.mas_equalTo(weakSelf.titleLabel2.mas_bottom).offset(12);
                }];
            }
            
            NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"ai_loading.gif" ofType:nil];
            NSData *imageData = [NSData dataWithContentsOfFile:filePath];
            self.loadingImageView.image = [UIImage sd_imageWithGIFData:imageData];
            self.loadingImageView.hidden = NO;
            
        }else{
            self.contentLabel2.hidden = NO;
            self.voiceImageView2.hidden = NO;
            self.cBtn.hidden = NO;
            self.shareBtn.hidden = NO;
            if(self.loadingImageView){
                self.loadingImageView.hidden = YES;
            }
        }
    }
    if(model.type == 1){
        self.tipLabel.hidden = NO;
        self.tipLabel.text = @"AI翻译";
        self.titleLabel2.text = @"AI翻译";
    }else if (model.type == 2){
        self.tipLabel.hidden = NO;
        self.tipLabel.text = @"句子纠错";
        self.titleLabel2.text = @"正确表达";
    }else if (model.type == 3){
        self.tipLabel.hidden = NO;
        self.tipLabel.text = @"语法分析";
        self.titleLabel2.text = @"语法分析";
    }else if (model.type == 4){
        self.tipLabel.hidden = NO;
        self.tipLabel.text = @"词文串学";
        self.titleLabel2.text = @"生成语句";
    }else{
        self.tipLabel.text = @"";
        self.tipLabel.hidden = YES;
        self.titleLabel2.text = @"结果";
    }
    if(self.tipLabel.hidden == NO){
        CGSize t_size = [self.tipLabel sizeThatFits:CGSizeMake(MAXFLOAT,23)];
        self.tipLabel.frame = CGRectMake(self.currentAvailableWidth-32-t_size.width-20,0,t_size.width+20,23);
        [self.tipLabel addRoundedCorners:UIRectCornerBottomLeft|UIRectCornerTopRight withRadii:CGSizeMake(10,10)];
    }
    self.contentLabel.text = model.sendContent;
    CGSize c_size = [self.contentLabel sizeThatFits:CGSizeMake(self.currentAvailableWidth-64,MAXFLOAT)];
    [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(c_size.height);
    }];
    [self.voiceImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.contentLabel.mas_bottom).offset(12);
    }];
    [self.line mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.voiceImageView.mas_bottom).offset(12);
    }];
    if(model.isLoad == YES){
        [self.loadingImageView mas_updateConstraints:^(MASConstraintMaker *make){
            make.top.mas_equalTo(weakSelf.titleLabel2.mas_bottom).offset(12);
        }];
    }else{
        self.contentLabel2.text = model.receiveContent;
        CGSize c_size2 = [self.contentLabel2 sizeThatFits:CGSizeMake(self.currentAvailableWidth-64,MAXFLOAT)];
        [self.contentLabel2 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(c_size2.height);
            make.top.mas_equalTo(weakSelf.titleLabel2.mas_bottom).offset(12);
        }];
        [self.voiceImageView2 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(weakSelf.contentLabel2.mas_bottom).offset(12);
        }];
    }
    
}

#pragma mark - 开始播放动画
- (void)startVoiceAnimation{
    NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"voice_play.gif" ofType:nil];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    self.voiceImageView.image = [UIImage sd_imageWithGIFData:imageData];
}

- (void)startVoiceAnimation2{
    NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"voice_play.gif" ofType:nil];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    self.voiceImageView2.image = [UIImage sd_imageWithGIFData:imageData];
}

#pragma mark - 结束播放动画
- (void)stopVoiceAnimation{
    self.voiceImageView.image = [UIImage imageNamed:@"mint_mycollect_play"];
}

- (void)stopVoiceAnimation2{
    self.voiceImageView2.image = [UIImage imageNamed:@"mint_mycollect_play"];
}


@end
